﻿using MyClassLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProgram
{
    public partial class FrmDrugEdit : Form
    {
        global::MyDB.MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();
        public FrmDrugEdit()
        {
            InitializeComponent();
        }
        ClsDrug clsdrug = new ClsDrug();
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        int ID = ClsDrug.editID;
        private void FrmDrugScheduleEdit_Load(object sender, EventArgs e)
        {
            clsdrug = new DrugFrequency();
            comboBox4.DataSource = clsdrug.GetData();
            comboBox4.DisplayMember = "用藥頻率";

            clsdrug = new TotalDrug();
            comboBox3.DataSource = clsdrug.GetData();
            comboBox3.DisplayMember = "中文名稱";

            var q = DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First();
            comboBox3.SelectedIndex = (int)q.藥物ID- 1;
            comboBox4.SelectedIndex = (int)q.用藥頻率ID - 1;
            起始日期DateTimePicker.Value = q.起始日期;
            結束日期DateTimePicker.Value = (DateTime)q.結束日期;
            備註TextBox.Text = q.備註;
            結案原因TextBox.Text = q.結案原因;
            結案CheckBox.Checked = (bool)q.結案;


        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyDB.DrugSchedule i = new MyDB.DrugSchedule
            {
                藥物ID = ((MyDB.DrugOverview)comboBox3.SelectedItem).藥物ID,
                用藥頻率ID = ((MyDB.DrugFrequency)comboBox4.SelectedItem).用藥頻率ID,
                起始日期 = 起始日期DateTimePicker.Value,
                結束日期 = 結束日期DateTimePicker.Value,
                備註 = 備註TextBox.Text,
                結案原因 = 結案原因TextBox.Text,
                結案 = 結案CheckBox.Checked
            };

            try
            {
                DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().起始日期 = i.起始日期;
                DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().備註 = i.備註;
                DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().結束日期 = i.結束日期;
                DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().結案 = i.結案;
                DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().結案原因 = i.結案原因;
                DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().藥物ID = i.藥物ID;
                DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().用藥頻率ID = i.用藥頻率ID;
                DB.SubmitChanges();

                MessageBox.Show("修改成功!");
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("修改失敗");
            }

            ClsRecord.Record(sender, this.Text, DB.DrugSchedules.Where(n => n.用藥排程ID == ID).First().Diagnosis.RegisterInformation.Patients.病患ID);
        }
    }
}
